<!DOCTYPE html>
<html>
<head>
    <title>Physics Diagram - Bent Pipe</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }
        canvas {
            border: 1px solid #ccc;
            background-color: white;
        }
    </style>
</head>
<body>
<canvas id="canvas" width="450" height="400"></canvas>
<script>
    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');

    // Clear canvas
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    // Define geometry and styles
    const cx = 160; // Center X of the bend arc
    const cy = 230; // Center Y of the bend arc
    const r_out = 90; // Outer radius
    const r_in = 60;  // Inner radius
    const pipeWidth = r_out - r_in;
    const verticalLength = 120;
    const horizontalLength = 150;

    const topY = cy - verticalLength;
    const rightX = cx + horizontalLength;
    const mainLineWidth = 2;

    ctx.strokeStyle = 'black';
    ctx.lineWidth = mainLineWidth;

    // --- Draw Pipe Outline ---
    // Outer arc
    ctx.beginPath();
    ctx.arc(cx, cy, r_out, 1 * Math.PI, 1.5 * Math.PI);
    ctx.stroke();

    // Inner arc
    ctx.beginPath();
    ctx.arc(cx, cy, r_in, 1 * Math.PI, 1.5 * Math.PI);
    ctx.stroke();

    // Vertical straight part
    ctx.beginPath();
    ctx.moveTo(cx - r_out, cy);
    ctx.lineTo(cx - r_out, topY);
    ctx.stroke();
    ctx.beginPath();
    ctx.moveTo(cx - r_in, cy);
    ctx.lineTo(cx - r_in, topY);
    ctx.stroke();

    // Horizontal straight part
    ctx.beginPath();
    ctx.moveTo(cx, cy - r_out);
    ctx.lineTo(rightX, cy - r_out);
    ctx.stroke();
    ctx.beginPath();
    ctx.moveTo(cx, cy - r_in);
    ctx.lineTo(rightX, cy - r_in);
    ctx.stroke();


    // --- Draw Streamlines ---
    ctx.lineWidth = 1;
    const numStreamlines = 5;
    const streamSpacing = pipeWidth / (numStreamlines + 1);
    for (let i = 1; i <= numStreamlines; i++) {
        const r = r_in + i * streamSpacing;
        // Arc
        ctx.beginPath();
        ctx.arc(cx, cy, r, 1 * Math.PI, 1.5 * Math.PI);
        ctx.stroke();
        // Vertical
        ctx.beginPath();
        ctx.moveTo(cx - r, cy);
        ctx.lineTo(cx - r, topY);
        ctx.stroke();
        // Horizontal
        ctx.beginPath();
        ctx.moveTo(cx, cy - r);
        ctx.lineTo(rightX, cy - r);
        ctx.stroke();
    }
    ctx.lineWidth = mainLineWidth; // Reset


    // --- Helper function to draw arrows ---
    function drawArrow(ctx, fromx, fromy, tox, toy, headlen = 10) {
        const angle = Math.atan2(toy - fromy, tox - fromx);
        ctx.beginPath();
        ctx.moveTo(fromx, fromy);
        ctx.lineTo(tox, toy);
        ctx.moveTo(tox, toy);
        ctx.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6));
        ctx.moveTo(tox, toy);
        ctx.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6));
        ctx.stroke();
    }

    // --- Draw Arrows and Labels ---
    // Incoming flow arrow
    const inArrowX = cx - (r_in + r_out) / 2;
    drawArrow(ctx, inArrowX, topY - 30, inArrowX, topY, 12);

    // Outgoing flow arrow and label 'v'
    const outArrowY = cy - (r_in + r_out) / 2;
    drawArrow(ctx, rightX + 10, outArrowY, rightX + 50, outArrowY, 12);
    ctx.fillStyle = 'black';
    ctx.font = 'italic 24px "Times New Roman"';
    ctx.fillText('v', rightX + 60, outArrowY + 8);


    // --- Draw Dashed Lines and Right Angle ---
    ctx.save();
    ctx.setLineDash([5, 3]);
    ctx.lineWidth = 1.5;
    ctx.beginPath();
    ctx.moveTo(cx - r_out, cy);
    ctx.lineTo(cx, cy);
    ctx.lineTo(cx, cy - r_out);
    ctx.stroke();
    ctx.restore();

    // Right angle symbol
    ctx.lineWidth = 1.5;
    ctx.beginPath();
    ctx.moveTo(cx - 12, cy);
    ctx.lineTo(cx - 12, cy - 12);
    ctx.lineTo(cx, cy - 12);
    ctx.stroke();


    // --- Draw Cross-section and label 'S' ---
    ctx.beginPath();
    ctx.ellipse(rightX, outArrowY, 6, pipeWidth / 2, 0, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.font = 'italic 24px "Times New Roman"';
    ctx.fillText('S', rightX, cy - r_out - 10);


    // --- Draw Labels 'A' and 'B' ---
    ctx.font = 'italic 24px "Times New Roman"';
    ctx.fillText('A', cx - r_out - 30, cy - r_out + 30);
    ctx.fillText('B', cx + 20, cy - r_out + 25);


    // --- Draw Bottom Caption ---
    ctx.font = '22px "SimSun"';
    ctx.fillStyle = 'black';
    ctx.textAlign = 'center';
    ctx.fillText('题 9 图', canvas.width / 2, canvas.height - 20);

</script>
</body>
</html>